package Login;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class zhuce {
	private JFrame frame = new JFrame("注册");
	private GridBagLayout gbl = new GridBagLayout();
	private GridBagConstraints gbc = new GridBagConstraints();
	JComboBox<String> comboBox;
	JLabel labpass1,labpass2;
	public Map<String, MobileCard> cards;//所有用户卡号
	JButton button1=new JButton();
	JPasswordField textword1;
	JPasswordField textword2;

	public static final String OBJ_PATH = "D:" + File.separator + "wuziqi" + File.separator + "CardUtil.obj";
	{
		cards = new HashMap<>();
	}


	// 1.在类中定义初始化界面的方法；
	public void initUI() {
		frame.setLayout(gbl); // 设定框架的布局模式
		// 设置窗体的大小
		frame.setBounds(700,500,600, 1000);
		// 设置窗体的关闭操作
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


		// 手机号选取
		JLabel labName = new JLabel("可选择的账号：");
		labName.setFont(new Font("楷体", Font.BOLD, 25));

		comboBox=new JComboBox<>();
		String[] newNumbers = getNewNumbers(9);
		for (int i=0;i<9;i++){
			comboBox.addItem(newNumbers[i]);
		}
		Dimension dime = new Dimension(200,30);
		//设置除顶级容器组件其他组件的大小
		comboBox.setPreferredSize(dime);
		comboBox.setFont(new Font("楷体", Font.BOLD, 25));

		//密码
		labpass1= new JLabel("密码：");
		labpass1.setFont(new Font("楷体", Font.BOLD, 25));
		frame.add(labpass1);
		//实例化JPasswordField
		textword1=new JPasswordField();
		//设置大小
		textword1.setPreferredSize(dime);//设置组件大小
		textword1.setFont(new Font("楷体", Font.BOLD, 25));
		//添加textword到窗体上
		frame.add(textword1);

		//密码验证
		labpass2= new JLabel("再次输入密码：");
		labpass2.setFont(new Font("楷体", Font.BOLD, 25));
		frame.add(labpass2);
		textword2=new JPasswordField();
		//设置大小
		textword2.setPreferredSize(dime);
		textword2.setFont(new Font("楷体", Font.BOLD, 25));
		frame.add(textword2);


		//设置按钮的显示内容
		Dimension dim1 = new Dimension(100,30);
		button1.setFont(new Font("楷体", Font.BOLD, 25));
		button1.setText("注册");
		//设置按钮的大小
		button1.setSize(dim1);

		gbc.fill = GridBagConstraints.BOTH; // 伸缩首先规定是两个方向上的
		gbc.weighty = 1; // 纵向比例都是1:1



		// 手机号选择
		gbc.gridwidth = 2;
		gbc.weightx = 1;
		gbl.setConstraints(labName, gbc);
		frame.add(labName);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbl.setConstraints(comboBox, gbc);
		frame.add(comboBox);

		// 填写密码
		gbc.gridwidth = 2;
		gbc.weightx = 1;
		gbl.setConstraints(labpass1, gbc);
		frame.add(labpass1);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbl.setConstraints(textword1, gbc);
		frame.add(textword1);

		// 验证密码
		gbc.gridwidth = 2;
		gbc.weightx = 1;
		gbl.setConstraints(labpass2, gbc);
		frame.add(labpass2);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbl.setConstraints(textword2, gbc);
		frame.add(textword2);


		// 注册
		gbl.setConstraints(button1, gbc);
		frame.add(button1);

		init();
		frame.pack();
		frame.setVisible(true);

	}
	public void init() {
		button1.addActionListener(new ActionListener() {//添加监听器
			//将用户名和密码写入文件中的操作
			@Override
			public void actionPerformed(ActionEvent e) {
				String pass1 = String.valueOf(textword1.getPassword());
				String pass2 = String.valueOf(textword2.getPassword());
				if(pass1.equals(pass2)){
					//创建卡号对象，放入数据
					MobileCard card = new MobileCard();
					card.cardNumber = (String) comboBox.getSelectedItem();
					card.passWord = String.valueOf(textword1.getPassword());


					//添加到cards中
					login.cardUtil.cards.put(card.cardNumber, card);
					login.saveData();
					System.out.println(card.cardNumber);
					JOptionPane.showMessageDialog(null, "注册成功！");
					frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				}else  {
					JOptionPane.showMessageDialog(null, "两次密码不一致！");
				}
			}
		});
	}

	public String createNumber() {
		StringBuilder sb = new StringBuilder("139");
		while (true) {
			for (int i = 0; i < 8; i++)
				sb.append((int) (Math.random() * 10) + "");
			//若该卡号无用户使用，可以离开循环
			if (cards.get(sb.toString()) == null) break;
			sb = new StringBuilder("139");
		}
		return sb.toString();
	}

	//根据参数获取指定的若干手机号，随机生成139开头、且不重复的
	public String[] getNewNumbers(int count) {
		String[] numbers = new String[count];

		mark:
		for (int i = 0; i < numbers.length; i++) {
			String number = createNumber();
			//检查已生成的是否有重复，有则重新生成
			for (int j = 0; j < i; j++) {
				//若有重复项，无需再继续比对，外循环重新再执行一次
				if (numbers[j].equals(number)) {
					i--;
					continue mark;//跳到label标签位置，跳过剩余代码
				}
			}
			numbers[i] = number;
		}
		return numbers;
	}
}



























